package com.learn.core.service.impl;

import com.learn.core.domain.LCoursewareType;
import com.learn.core.domain.LDictType;
import com.learn.core.mapper.LCoursewareTypeMapper;
import com.learn.core.mapper.LDictTypeMapper;
import com.learn.core.service.ILCoursewareTypeService;
import com.learn.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther CodeGenerator
 * @create 2021-03-13 19:39:45
 * @describe 课件分类表服务实现类
 */
@Service
public class LCoursewareTypeServiceImpl extends BaseServiceImpl<LCoursewareTypeMapper, LCoursewareType> implements ILCoursewareTypeService {


    @Autowired
    LCoursewareTypeMapper lCoursewareTypeMapper;

    @Override
    public List<LCoursewareType> queryLCoursewareTypeAll() {
        List<LCoursewareType> treeMenus =new ArrayList<>();
        List<LCoursewareType> lCoursewareTypeList = lCoursewareTypeMapper.selectList(null);
        // 查询树结构
        for(LCoursewareType lCoursewareType : getDictTypeNode(lCoursewareTypeList)) {
            lCoursewareType=buildChilTree(lCoursewareType,lCoursewareTypeList);
            treeMenus.add(lCoursewareType);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private LCoursewareType buildChilTree(LCoursewareType pNode,List<LCoursewareType> lCoursewareTypeList){
        List<LCoursewareType> chilCoursewareType =new ArrayList<>();
        for(LCoursewareType menuNode : lCoursewareTypeList) {
            if(menuNode.getParentNode() == pNode.getCoursewareTypeId()) {
                chilCoursewareType.add(buildChilTree(menuNode,lCoursewareTypeList));
            }
        }
        pNode.setChilCoursewareType(chilCoursewareType);
        return pNode;
    }

    //获取根节点
    private List<LCoursewareType> getDictTypeNode(List<LCoursewareType> lCoursewareTypes) {
        List<LCoursewareType> lCoursewareTypeList =new  ArrayList<LCoursewareType>();
        for(LCoursewareType lCoursewareType : lCoursewareTypes) {
            if(lCoursewareType.getParentNode() == 0) {
                lCoursewareTypeList.add(lCoursewareType);
            }
        }
        return lCoursewareTypeList;
    }
}
